package uk.ac.ebi.bioinvindex.dao.ejb3;

import uk.ac.ebi.bioinvindex.dao.ReferenceSourceDAO;
import uk.ac.ebi.bioinvindex.model.impl.xref.ReferenceSourceImpl;

import javax.persistence.EntityManager;
import java.util.List;

public class ReferenceSourceEJB3DAO extends IdentifiableEJB3DAO<ReferenceSourceImpl>
		implements ReferenceSourceDAO {

	public ReferenceSourceEJB3DAO() {
	}

	public ReferenceSourceEJB3DAO(EntityManager entityManager) {
		super(ReferenceSourceImpl.class, entityManager);
	}

	public ReferenceSourceImpl getReferenceSourceByName(String name) {
		List results = entityManager.createQuery(
				"select r from ReferenceSourceImpl r where name = :name").
				setParameter("name", name).getResultList();

		if (results.size() != 1) {
			return null;
//			throw new BIIDAOException("Reference source does not exist or more then one source " +
//					"correspond to a name : " + name);
		}

		return (ReferenceSourceImpl) results.get(0);
	}

	public ReferenceSourceImpl getReferenceSourceByAcc(String acc) {
		List results = entityManager.createQuery(
				"select r from ReferenceSourceImpl r where acc = :acc").
				setParameter("acc", acc).getResultList();

		if (results.size() != 1) {
			return null;
//			throw new BIIDAOException("Reference source does not exist or more then one source " +
//					"correspond to a name : " + acc);
		}

		return (ReferenceSourceImpl) results.get(0);
	}

}
